const path = require("path");
const webpack = require("webpack");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = ["js", "css"];
function resolve(dir) {
  return path.join(__dirname, dir);
}
const isProd = process.env.NODE_ENV === "production";
let publicPath = "/";
let plugins = [new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)];
const compressionPlugins = [
  new CompressionWebpackPlugin({
    algorithm: "gzip",
    test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
    threshold: 10240,
    minRatio: 0.8
  }),
  new webpack.optimize.LimitChunkCountPlugin({
    maxChunks: 5,
    minChunkSize: 100
  })
];
if (isProd) {
  plugins = plugins.concat(compressionPlugins);
  publicPath = process.env.VUE_APP_BASE;
}
const assetsCDN = {
  externals: {
    vuex: "Vuex",
    axios: "axios"
  },
  css: [],
  js: [
    "https://cdn.bootcdn.net/ajax/libs/vue-router/3.1.3/vue-router.min.js",
    "https://cdn.bootcdn.net/ajax/libs/axios/0.19.0/axios.min.js"
  ]
};

// vue.config.js
const vueConfig = {
  publicPath,
  configureWebpack: {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@i": path.resolve(__dirname, "./src/assets")
      }
    },
    plugins,
    externals: isProd ? assetsCDN.externals : {}
  },
  chainWebpack: config => {
    config.resolve.alias.set("@$", resolve("src"));
    if (isProd) {
      config.plugin("html").tap(args => {
        args[0].cdn = assetsCDN;
        return args;
      });
    }
  },

  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true
        }
      }
    }
  },

  devServer: {
    port: 8000,
    // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
    proxy: {
      "/manager": {
        target: "http://172.16.0.214:8083/manager",
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          "^/manager": ""
          // pathRewrite: {'^/api': '/'} 重写之后url为 http://192.168.1.16:8085/xxxx
          // pathRewrite: {'^/api': '/api'} 重写之后url为 http://192.168.1.16:8085/api/xxxx
        }
      }
    }
  },
  outputDir: process.env.VUE_APP_OUTDIR,
  productionSourceMap: false,
  lintOnSave: true,
  transpileDependencies: []
};

module.exports = vueConfig;
